package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.RoomDao;
import com.hziee.graduate.ticket.dao.RoomRecordDao;
import com.hziee.graduate.ticket.dao.SeatDao;
import com.hziee.graduate.ticket.dao.SeatScheduleDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Room;
import com.hziee.graduate.ticket.entity.RoomRecord;
import com.hziee.graduate.ticket.entity.Seat;
import com.hziee.graduate.ticket.entity.SeatSchedule;
import com.hziee.graduate.ticket.service.RoomService;
import com.hziee.graduate.ticket.utils.DateUtils;
import com.hziee.graduate.ticket.vo.RoomVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service("roomService")
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomDao, Room> implements RoomService {

    @Resource
    private RoomDao roomDao;
    @Resource
    private SeatDao seatDao;
    @Resource
    private RoomRecordDao roomRecordDao;

    @Override
    @Transactional
    public R<Object> addARoom(Map<String, Object> map) {
        Room room = new Room();
        int row = Integer.parseInt(map.get("roomRow").toString());
        int col = Integer.parseInt(map.get("roomCol").toString());
        room.setCinemaId(Long.parseLong(map.get("unitId").toString()));
        room.setRoomName(map.get("roomName").toString());
        room.setRoomRow(row);
        room.setRoomCol(col);
        room.setRoomType(Integer.parseInt(map.get("roomType").toString()));
        room.setRoomContain(Integer.parseInt(map.get("roomRow").toString()) * Integer.parseInt(map.get("roomCol").toString()));
        room.setRoomOpen(Integer.parseInt(map.get("roomOpen").toString()));
        room.setRoomFloor(Integer.parseInt(map.get("roomFloor").toString()));
        room.setCreateTime(DateUtils.getCurrentYMDStr());
        int insert = roomDao.insert(room);
        if(insert > 0) {
            List<Seat> seats = new ArrayList<Seat>();
            for (int i = 1; i <= row; i++) {
                for (int j = 1; j <= col; j++) {
                    seats.add(new Seat(room.getRoomId(),i, j,1,row,col));
                }
            }
            int insertBatch = seatDao.insertBatch(seats);
            if(insertBatch > 0) {
                log.debug("座位插入成功");
                return R.success("影厅插入成功");
            }
        }else{
            return R.success("影厅插入失败");
        }
        return null;
    }

    @Override
    public R<List<Room>> getAllRoomByCinema(Long cinemaId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cinema_id", cinemaId);
        map.put("del_flag", 0);
        List<Room> rooms = roomDao.selectByMap(map);
        return R.success(rooms);
    }

    @Override
    public R<RoomVO> getRoomBySchedule(Long movieId, String date, Long cinemaId, Long scheduleId) {
        RoomVO roomBySchedule = roomDao.getARoomBySchedule(movieId, date, cinemaId, scheduleId);
        return R.success(roomBySchedule);
    }

    @Override
    @Transactional
    public R<Object> updateARoom(Map<String, Object> map) {
        // 获取原有的影院
        Long roomId = Long.parseLong(map.get("roomId").toString());
        Room roomById = roomDao.selectById(roomId);
        // 更新所有属性
        int row = Integer.parseInt(map.get("roomRow").toString());
        int col = Integer.parseInt(map.get("roomCol").toString());
        roomById.setCinemaId(Long.parseLong(map.get("unitId").toString()));
        roomById.setRoomName(map.get("roomName").toString());
        roomById.setRoomRow(row);
        roomById.setRoomCol(col);
        roomById.setRoomType(Integer.parseInt(map.get("roomType").toString()));
        roomById.setRoomContain(Integer.parseInt(map.get("roomRow").toString()) * Integer.parseInt(map.get("roomCol").toString()));
        roomById.setRoomOpen(Integer.parseInt(map.get("roomOpen").toString()));
        roomById.setRoomFloor(Integer.parseInt(map.get("roomFloor").toString()));
        roomDao.update(roomById, new UpdateWrapper<Room>().eq("room_id", roomId));
        // 删除原有座位信息
        int deleteRoomById = seatDao.delete(new QueryWrapper<Seat>().eq("room_id", roomId));
        // 重新添加座位
        List<Seat> seats = new ArrayList<Seat>();
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= col; j++) {
                seats.add(new Seat(roomId,i, j,1,row,col));
            }
        }
        int insertBatch = seatDao.insertBatch(seats);
        // 标记这条记录已完成
        RoomRecord roomRecord = roomRecordDao
                .selectOne(new QueryWrapper<RoomRecord>()
                .eq("room_id",roomId)
                .eq("state",1)
                .eq("done",0)
                .eq("opt_type",0)
       );
        roomRecord.setDone(1);
        roomRecord.setOptType(1);
        int room_record_id = roomRecordDao.update(roomRecord, new UpdateWrapper<RoomRecord>().eq("room_record_id", roomRecord.getRoomRecordId()));
        return R.success();
    }

    @Override
    @Transactional
    public R<Object> deleteARoom(Long roomId) {
        // 更新记录值
        RoomRecord roomRecord = roomRecordDao
                .selectOne(new QueryWrapper<RoomRecord>()
                        .eq("room_id",roomId)
                        .eq("state",1)
                        .eq("done",0)
                        .eq("opt_type",0)
                );
        roomRecord.setDone(1);
        roomRecord.setOptType(2);
        int room_record_id = roomRecordDao.update(roomRecord, new UpdateWrapper<RoomRecord>().eq("room_record_id", roomRecord.getRoomRecordId()));
        // 删除字段
        Room room = new Room();
        room.setDelFlag(1);
        int room_id = roomDao.update(room, new UpdateWrapper<Room>().eq("room_id", roomId));
        return R.success();
    }

    @Override
    public R<List<Room>> getAvailableRoomsByCinema(Long cinemaId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cinema_id", cinemaId);
        map.put("del_flag", 0);
        map.put("room_open", 1);
        List<Room> rooms = roomDao.selectByMap(map);
        return R.success(rooms);
    }

    @Override
    public R<Object> updateRoomOpenStatusById(Long roomId) {
        boolean update = update().eq("room_id", roomId).set("room_open", 1).update();
        return R.success();
    }
}

